package com.woniuxy;

import com.alibaba.cloud.ai.memory.redis.RedisChatMemoryRepository;
import com.woniuxy.tools.WeatherService;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemoryRepository;
import org.springframework.ai.chat.memory.InMemoryChatMemoryRepository;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
//import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;

@SpringBootApplication
@EnableCaching
//@EnableElasticsearchRepositories
public class MyApp {
    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }
    //配置AI的聊天客户端，全局，Builder自动注入了ChatModel
    @Bean
    public ChatClient chatClient(ChatClient.Builder builder,MessageChatMemoryAdvisor messageChatMemoryAdvisor, WeatherService weatherService) {
        //系统提示词
        return builder.defaultSystem("你是蜗牛AI客服小姐姐，提高专业的知识，和本地的RAG数据，一起优化后回答业务问题。")
                .defaultTools(weatherService) //天气，时间，其他小工具
                .defaultAdvisors(messageChatMemoryAdvisor) //AI的记忆
                .build();
    }
    //形参是多态：暂时全局只有RedisChatMemoryRepository ，所以这里没报错，如果以后还需要加入mem0这种聊天记忆
    @Bean
    public MessageChatMemoryAdvisor messageChatMemoryAdvisor(ChatMemoryRepository chatMemoryRepository) {
        return MessageChatMemoryAdvisor.builder(
                MessageWindowChatMemory.builder()
                        .chatMemoryRepository(chatMemoryRepository)
                        .build())
                .build();
    }
    //第一个基于Redis的聊天记忆
    @Bean
    public ChatMemoryRepository chatMemoryRepository(RedisProperties redisProperties) {
        return RedisChatMemoryRepository.builder()
                .host(redisProperties.getHost())
                .port(redisProperties.getPort())
                .password(redisProperties.getPassword())
                .build();
    }
    //第二个基于内存的聊天记忆
    @Bean
    public ChatMemoryRepository inMemoryChatMemoryRepository() {
        return new InMemoryChatMemoryRepository();
    }
}
